package com.gxa.community.admin.exeption;
import com.gxa.community.core.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;

import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.stream.Collectors;

@RestControllerAdvice
@Slf4j
public class qjException {

    @ExceptionHandler(MissingServletRequestParameterException.class)
    public R HttpMessageNotReadableException (HttpMessageNotReadableException httpMessageNotReadableException){
        String message = httpMessageNotReadableException.getMessage();
        log.info("MissingServletRequestParameterException:进来了");
        return R.error(message);
    }


    /**
     * MethodArgumentNotValidException捕获
     * @param e
     * @return
     */
    @ExceptionHandler({MethodArgumentNotValidException.class})
    public R Exception(MethodArgumentNotValidException e){
        log.info("捕获到了:MethodArgumentNotValidException");
        StringBuilder sb = new StringBuilder();
        List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
        String message = allErrors.stream().map(s -> s.getDefaultMessage()).collect(Collectors.joining(";"));
        return R.error(message);
    }
//    }
    /**
     * 单独传产接受参数异常
     * @param errors
     * @return
     */
    @ExceptionHandler({ConstraintViolationException.class})
    public R ConstraintViolationException(ConstraintViolationException errors){
        log.info("捕获到了:ConstraintViolationException");
        StringBuffer stringBuffer = new StringBuffer();//拼接
        for (ConstraintViolation<?> violation : errors.getConstraintViolations()) {
            stringBuffer.append(violation.getMessage());
        }
        log.error(stringBuffer.toString());
        return R.error(stringBuffer.toString());
    }
    /**
     * 对象接受参数异常
     * @param e
     * @return
     */
    @ExceptionHandler({BindException.class})
    public R BindException(Errors e){
        log.info("捕获到了:BindException");
        StringBuffer stringBuffer = new StringBuffer();
        for (ObjectError allError : e.getAllErrors()) {
            stringBuffer.append(allError.getDefaultMessage());
//            stringBuffer.append(allError.getDefaultMessage());
        }
        log.error(stringBuffer.toString());
        return R.error(stringBuffer.toString());
    }

    /**
     * 传参异常捕获
     * @return
     */
    @ExceptionHandler({HttpMessageNotReadableException.class})
    public R httpMessageNotReadableException(HttpMessageNotReadableException e){
        log.error("捕获到了:HttpMessageNotReadableException==详细error:{}",e.getMessage());
        return R.error("HttpMessageNotReadableException异常:传参参数类型不匹配");
    }


}
